<html>
<head><meta charset="utf-8"><title>Update-in-place vs mut? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Update-in-place.20vs.20mut.3F.html">Update-in-place vs mut?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="210170082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Update-in-place%20vs%20mut%3F/near/210170082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Update-in-place.20vs.20mut.3F.html#210170082">(Sep 15 2020 at 18:31)</a>:</h4>
<p>So <a href="https://github.com/rust-lang/rust/issues/76725">#76725</a> mentions a curious example of folding a Vec into existence that I thought was supposed to be semantically near-equivalent in Rust whether it was using mutation directly or using update-in-place. While I acknowledge the simple example would usually be replaced with <code>collect</code>, there are more complicated ones that don't yield as easily, yet would be equivalent. Am I misunderstanding something, or is this an optimization failure?</p>



<a name="210171338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Update-in-place%20vs%20mut%3F/near/210171338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Update-in-place.20vs.20mut.3F.html#210171338">(Sep 15 2020 at 18:40)</a>:</h4>
<p>I think it's both a missed optimization (ideally the accumulator would get loop-lifted) and non-idiomatic code.  To me it's an anti-pattern to always return the accumulator object in <code>fold</code>; in that case I think it's better to use an <code>FnMut</code> closure instead &lt;<a href="https://github.com/rust-lang/rust/pull/76746#issuecomment-692884123">https://github.com/rust-lang/rust/pull/76746#issuecomment-692884123</a>&gt;.</p>
<p>(The advantage of the accumulator in <code>fold</code> is that you can move it, which would be awkward in a stateful closure.  But if you only need to <code>&amp;</code> or <code>&amp;mut</code> it, I think it's better to close over it rather than pass it into the closure every time.)</p>



<a name="210172993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Update-in-place%20vs%20mut%3F/near/210172993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Update-in-place.20vs.20mut.3F.html#210172993">(Sep 15 2020 at 18:53)</a>:</h4>
<p>I find this pattern to be reasonably natural to myself, tbh, but I admit to having a perverse affection for long method chains.<br>
Is there a way to guarantee the accumulator does get loop lifted?</p>



<a name="210177689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Update-in-place%20vs%20mut%3F/near/210177689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Update-in-place.20vs.20mut.3F.html#210177689">(Sep 15 2020 at 19:27)</a>:</h4>
<p>the only guaranteed optimization is to do it yourself</p>



<a name="210187032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Update-in-place%20vs%20mut%3F/near/210187032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mark <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Update-in-place.20vs.20mut.3F.html#210187032">(Sep 15 2020 at 20:36)</a>:</h4>
<p>A less contrived example (although, admittedly, still contrived) was seeing things like <a href="https://exercism.io/tracks/rust/exercises/anagram/solutions/6cc11233635d41128eaaa2f9b3f78d9d">this Exercism solution</a> where there was a floating return of the accumulator in the closure. This felt like an equivalent to ruby's <a href="https://ruby-doc.org/core-2.4.1/Enumerable.html#method-i-each_with_object"><code>each_with_object</code></a> (although who's to say they did that right?)</p>



<a name="210195800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Update-in-place%20vs%20mut%3F/near/210195800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Update-in-place.20vs.20mut.3F.html#210195800">(Sep 15 2020 at 21:52)</a>:</h4>
<p>Yeah, I've seen that pattern a lot as well.<br>
I think what I really meant was "if one were to want to optimize this, where would they start?"</p>



<a name="210196503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Update-in-place%20vs%20mut%3F/near/210196503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Update-in-place.20vs.20mut.3F.html#210196503">(Sep 15 2020 at 21:58)</a>:</h4>
<p>maybe take a look at the MIR generated for <code>fold</code> and see why it's different from <code>fold_mut</code>?</p>



<a name="210196514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Update-in-place%20vs%20mut%3F/near/210196514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Update-in-place.20vs.20mut.3F.html#210196514">(Sep 15 2020 at 21:58)</a>:</h4>
<p><code>-Z unpretty=mir</code></p>



<a name="210199320"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Update-in-place%20vs%20mut%3F/near/210199320" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Update-in-place.20vs.20mut.3F.html#210199320">(Sep 15 2020 at 22:26)</a>:</h4>
<p>it's clear <em>why</em> they're different, moving <code>acc</code> around versus just <code>&amp;mut acc</code></p>



<a name="210199371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Update-in-place%20vs%20mut%3F/near/210199371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Update-in-place.20vs.20mut.3F.html#210199371">(Sep 15 2020 at 22:27)</a>:</h4>
<p>the question is what optimization can do with it, whether in LLVM or maybe someday in MIR</p>



<a name="210208975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Update-in-place%20vs%20mut%3F/near/210208975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Update-in-place.20vs.20mut.3F.html#210208975">(Sep 16 2020 at 00:23)</a>:</h4>
<p>Yeah, the "moving around the vec in the inner loop" is pretty clear in the assembly: <a href="https://rust.godbolt.org/z/zhrEzj">https://rust.godbolt.org/z/zhrEzj</a></p>



<a name="210211456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Update-in-place%20vs%20mut%3F/near/210211456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Update-in-place.20vs.20mut.3F.html#210211456">(Sep 16 2020 at 01:02)</a>:</h4>
<p>Well, I opened an issue for a clippy lint about it: <a href="https://github.com/rust-lang/rust-clippy/issues/6053">https://github.com/rust-lang/rust-clippy/issues/6053</a></p>
<p>(I don't know if it's even feasible to lint, given them semantic analysis of the closure needed, but...)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>